package ${baseInfo.packageName};

import lombok.Getter;
import lombok.ToString;

import ${baseColumnEnum.packageName}.${baseColumnEnum.fileName};

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;


/**
 * 针对表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的数据库字段名的枚举
 *
 * @author MyBatisX Generator
 */
@Getter
@ToString
public enum ${columnEnum.fileName} implements ${baseColumnEnum.fileName} {
<#assign counter = 1>
<#assign defaultValByType={
'String':'""',
'Integer':'0',
'Long':'0L',
'Date':'"0000-00-00 00:00:00"',
'BigDecimal':'BigDecimal.ZERO'
}>
<#list tableClass.allFields as field>
    /**
    * 描述：${field.remark!'无描述'}<br />
    * 数据库字段: ${counter}.${field.columnName}<br />
    * 允许为空: ${field.nullable?string('true','false')}<br />
     */
    ${field.columnName?upper_case}(${counter}, "${field.columnName}"),
<#assign counter = counter + 1>
</#list>
    ;
    /**
     * 表名
     */
    public static final String TABLE_NAME = "${tableClass.tableName}";
    /**
     * 编码
     */
    private final int code;

    /**
     * 字段名
     */
    private final String columnName;

    ${columnEnum.fileName}(int code, String columnName) {
        this.code = code;
        this.columnName = columnName;
    }

    /**
     * 根据编码查询枚举
     */
    public static Optional<${columnEnum.fileName}> byCode(Integer code) {
        if (null == code) {
            return Optional.empty();
        }
        for (${columnEnum.fileName} item : values()) {
            if (item.getCode() == code) {
                return Optional.of(item);
            }
        }
        return Optional.empty();
    }

    /**
     * 根据字段名查询枚举
     */
    public static Optional<${columnEnum.fileName}> byColumnName(String columnName) {
        if (null == columnName || columnName.trim().isEmpty()) {
            return Optional.empty();
        }
        for (${columnEnum.fileName} item : values()) {
            if (Objects.equals(item.getColumnName(), columnName)) {
                return Optional.of(item);
            }
        }
        return Optional.empty();
    }

    /**
     * 抽取字段名
     */
    public static ${'Collection<String>'} toColumnNames(${columnEnum.fileName}... columns) {
        if (null == columns || columns.length == 0) {
            return Collections.emptySet();
        }
        final ${'Collection<String>'} result = new HashSet<>(columns.length);
        for (${columnEnum.fileName} item : columns) {
            result.add(item.getColumnName());
        }
        return result;
    }
}
